import api from '@/api/device'

const state = {
  screen: [],
  showChild:false,
  file: {}
}

const mutations = {
  COMMOND_OF_SCREEN_DATA: (state, data) => {
    localStorage.setItem('screenData', data)
    state.screen = data
  },
  COMMOND_OF_FILE_DATA: (state, data) => {
    localStorage.setItem('fileData', data)
    state.file = data
  }
}

const actions = {
  // 终端树
  // 加载树
  doLoadTree({ commit }, params) {
    return new Promise(resolve => {
      api.loadTree(params).then(res => {
        resolve(res)
      })
    })
  },
  // 添加树节点
  doAddTreeNode({ commit }, params) {
    return new Promise(resolve => {
      api.addTreeNode(params).then(res => {
        resolve(res)
      })
    })
  },
  // 编辑树节点
  doEditTreeNode({ commit }, params) {
    return new Promise(resolve => {
      api.editTreeNode(params).then(res => {
        resolve(res)
      })
    })
  },
  // 删除树节点
  doDeleteTreeNode({ commit }, params){
    return new Promise(resolve => {
      api.deleteTreeNode(params).then(res => {
        resolve(res)
      })
    })
  },
  // 标签数
  // 加载树
  doLoadTagTree({ commit }, params) {
    return new Promise(resolve => {
      api.loadTagTree(params).then(res => {
        resolve(res)
      })
    })
  },
  // 主键查询分组添加过的标签
  doLoadTagListByID({ commit }, params) {
    return new Promise(resolve => {
      api.getTagListBy(params).then(res => {
        resolve(res)
      })
    })
  },
  doSaveTagByID({ commit }, params) {
    return new Promise(resolve => {
      api.saveTag(params).then(res => {
        resolve(res)
      })
    })
  },

  // 终端点击统计列表
  doTerminalClickList({ commit }, params) {
    return new Promise(resolve => {
      api.terminalClickList(params).then(res => {
        resolve(res)
      })
    })
  },

  // 统计点击 图表
  doClickStatisticsData({ commit }, params) {
    return new Promise(resolve => {
      api.clickStatisticsData(params).then(res => {
        resolve(res)
      })
    })
  },

  // 终端管理列表
  doDeviceList({ commit }, params) {
    return new Promise(resolve => {
      api.deviceList(params).then(res => {
        resolve(res)
      })
    })
  },
  // 删除终端
  doDeleteDevice({ commit }, params ) {
    return new Promise(resolve => {
      api.deleteDeviceInfo(params).then(res => {
        resolve(res)
      })
    })
  },
  // 添加终端
  doAddDevice({ commit }, params) {
    return new Promise(resolve => {
      api.addDeviceInfo(params).then(res => {
        resolve(res)
      })
    })
  },
  // 修改终端
  doUpdateDevice({ commit }, params) {
    return new Promise(resolve => {
      api.updateDeviceInfo(params).then(res => {
        resolve(res)
      })
    })
  },
  // 批量设置终端信息
  doSetDeviceInfo({ commit }, params) {
    return new Promise(resolve => {
      api.setDeviceInfo(params).then(res => {
        resolve(res)
      })
    })
  },
  // 根据ID获取终端信息
  doLoadDeviceByID({ commit }, params) {
    return new Promise(resolve => {
      api.getDeviceBy(params).then(res => {
        resolve(res)
      })
    })
  },
  // 终端审核
  doDeviceExamine({ commit }, params) {
    return new Promise(resolve => {
      api.deviceExamine(params).then(res => {
        resolve(res)
      })
    })
  },

  // 查询预警规则列表
  doWarnRuleList({ commit }, params ) {
    return new Promise(resolve => {
      api.warnRuleList(params).then(res => {
        resolve(res)
      })
    })
  },
  // 添加预警规则
  doAddWarnRule({ commit }, params) {
    return new Promise(resolve => {
      api.addWarnRule(params).then(res => {
        resolve(res)
      })
    })
  },
  // 编辑预警规则
  doEditWarnRule({ commit }, params) {
    return new Promise(resolve => {
      api.editWarnRule(params).then(res => {
        resolve(res)
      })
    })
  },
  // 删除预警规则
  doDeleteWarnRule({ commit }, params ) {
    return new Promise(resolve => {
      api.deleteWarnRule(params).then(res => {
        resolve(res)
      })
    })
  },
  // 查询有预警的终端列表
  doWarnTerminalList({ commit }, params ) {
    return new Promise(resolve => {
      api.warnTerminalList(params).then(res => {
        resolve(res)
      })
    })
  },
  // // 查询单个有预警的终端的详细信息
  // doWarnTerminalStatusInfo({ commit }, params ) {
  //   return new Promise(resolve => {
  //     api.warnTerminalStatus(params).then(res => {
  //       resolve(res)
  //     })
  //   })
  // },

  //查询终端升级文件列表
  doTerminalFilelist({ commit }, params ) {
    return new Promise(resolve => {
      api.terminalFilelist(params).then(res => {
        resolve(res)
      })
    })
  },
  // 上传文件
  uploadTerminalFile({ commit }, params ) {
    return new Promise(resolve => {
      api.uploadFile(params).then(res => {
        resolve(res)
      })
    })
  },
  // 删除文件
  deleteTerminalFile({ commit }, params ) {
    return new Promise(resolve => {
      api.deleteFile(params).then(res => {
        resolve(res)
      })
    })
  },

  commandUpgrade({ commit }, params ) {
    return new Promise(resolve => {
      api.commandUpgrade(params).then(res => {
        resolve(res)
      })
    })
  },

  // 查询终端列表
  deviceListByGroup({ commit }, params ) {
    return new Promise(resolve => {
      api.deviceListByGroup(params).then(res => {
        resolve(res)
      })
    })
  },

  // websocket数据存储
  saveWebsocketData({ commit }, data) {
    if(data) {
      const receiveJson = eval('(' + data + ')')
      const cmd = receiveJson.cmd
      const json = receiveJson.data
      // file
      if(cmd === 23) {
        commit('COMMOND_OF_FILE_DATA', JSON.stringify(json) )
      // screen
      }else if(cmd === 36) {
        commit('COMMOND_OF_SCREEN_DATA', JSON.stringify(json) )
      }
    }
  }
}

export default {
    namespaced: true,
    mutations,
    actions,
    state
  }
